/** 
 
*/
import React, { PureComponent } from 'react';
import { ModalC } from '@/src/components';
import { message } from 'antd';
import { connect } from 'dva';
import SPPITestRecordC from './SPPITestRecordC';

// visible
// title 标题
// type：1-新增  2-修改  3-给特定标的新增
// tenant 租户code exchangeCode tradingCode
// onCancel
// onOk
// otherModel model其他设置 应该用不到

class sppiTest extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      ref: null,
      selectSecurity: {},
    };
  }

  addSPPI = () => {
    const { form } = this.state.ref?.props;
    const { onOk, type, exchangeCode, tradingCode } = this.props;
    const { selectSecurity } = this.state;
    const { getFieldsValue } = form;
    const { securityId, ...payload } = getFieldsValue();
    this.props.dispatch({
      type: 'sppiTestRecordM/sppiAdd',
      payload: {
        ...payload,
        exchangeCode: type == 3 ? exchangeCode : selectSecurity?.exchangeCode,
        tradingCode: type == 3 ? tradingCode : selectSecurity?.tradingCode,
      },
      callback: (res) => {
        if (res.msg == '成功') {
          message.success('添加成功');
          onOk && onOk(payload.rgpd);
        }
      },
    });
  };

  editSPPI = () => {
    const { form } = this.state.ref?.props;
    const { onOk } = this.props;
    const { getFieldsValue } = form;
    const { securityId, ...payload } = getFieldsValue();
    const { exchangeCode, tradingCode } = this.props;

    this.props.dispatch({
      type: 'sppiTestRecordM/sppiAdd',
      payload: {
        ...payload,
        exchangeCode,
        tradingCode,
      },
      callback: (res) => {
        if (res.msg == '成功') {
          message.success('修改成功');
          onOk && onOk();
        }
      },
    });
  };

  onOk = () => {
    const { form } = this.state.ref?.props;
    const { onOk, type } = this.props;
    const { getFieldsValue } = form;
    const { xtpd, securityId } = getFieldsValue();
    if (!securityId) {
      message.error('请选择证券代码');
      return;
    }
    if (xtpd) {
      if (type == 1) {
        this.addSPPI();
      }
      if (type == 2) {
        this.editSPPI();
      }
      if (type == 3) {
        this.addSPPI();
        // onOk && onOk(xtpd)
      }
    } else {
      message.error('请设置SPPI测试系统判定条件');
    }
  };

  render() {
    const { title, visible = false, exchangeCode, tradingCode, onCancel = () => {}, type, otherModel, tenant, loading } = this.props;

    return (
      <ModalC
        title={title}
        visible={visible}
        maskClosable={false}
        destroyOnClose={true}
        confirmLoading={loading.effects['sppiTestRecordM/add']}
        onOk={this.onOk}
        onCancel={onCancel}
        {...otherModel}
      >
        <SPPITestRecordC
          form={this.props.form}
          tenant={tenant}
          exchangeCode={exchangeCode}
          tradingCode={tradingCode}
          type={type}
          onRef={(e) => this.setState({ ref: e })}
          onSecurityChange={(e) => {
            this.setState({
              selectSecurity: e || {},
            });
          }}
        />
      </ModalC>
    );
  }
}
export default connect(({ sppiTestRecordM, loading }) => ({
  sppiTestRecordM,
  loading,
}))(sppiTest);
